<?php

/**
 * @Author: kaihui.wang
 * @Contact  hpuwang@gmail.com
 * @Version: 1.0.0
 * @Date: 2020/3/26 14:07
 */

namespace Eduxplus\WebsiteBundle\Controller\Web;

use Eduxplus\CoreBundle\Lib\Base\BaseHtmlController;
use Eduxplus\WebsiteBundle\Service\BannerService;
use Eduxplus\WebsiteBundle\Service\GlobService;
use Eduxplus\WebsiteBundle\Service\TutorialService;
use Eduxplus\WebsiteBundle\Service\NewsService;
use Eduxplus\WebsiteBundle\Service\PageService;
use Eduxplus\WebsiteBundle\Service\MsgService;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

class IndexController extends BaseHtmlController
{

    
    public function indexAction(BannerService $bannerService, NewsService $newsService, TutorialService $tutorialService, GlobService $globService)
    {
        $banners = $bannerService->getBanners(0);//首页banner
        $news = $newsService->getNewsByTopValue(2, 10); //精选文章
        $notices = $newsService->getNewsByCategory(1, 3);//公告
        $newsList = $newsService->getTopNews(10);// 最新文章
        $newestTutorials = $tutorialService->getNewestTutorials(6);//精选教程

        $data = [];
        $data['banners'] = $banners;
        $data['news'] = $news;
        $data['notices'] = $notices;
        $data['newsList'] = $newsList;
        $data['newestTutorials'] = $newestTutorials;

        $data['copyright'] = $newsService->getOption("app.copyright");
        $data['beian'] = $newsService->getOption("app.beian.number");
        $data['oneSentenceADay']= $globService->getOneSentenceADay();
        
        $data['seoTitle']="首页";
        $data['seoDescr'] = $newsService->getOption("app.seo.homepage.descr");
        $data['seoKw'] = $newsService->getOption("app.seo.homepage.keyword");
        $data['route'] = "app_index";
        
        return $this->render('@WebsiteBundle/index/index.html.twig', $data);
    }


    public function headerAction($route="app_index", $sidebar="0", MsgService $msgService, RequestStack $request){
        $user = $this->getUser();
        $data = [];
        $data['userinfo'] = $user;
        $data['msgUnreadCount'] = 0;
        if($user){
            $uid = $this->getUid();
            $count =  $msgService->msgUnReadCount($uid);
            $data['msgUnreadCount'] = $count>99?"99+":$count;
        }
        $request = $request->getMainRequest()?$request->getMainRequest():$request;
        
        $mobile = $request->cookies->get("site_login_mobile");
        $goto = $request->get("goto");
        $kw = $request->get("kw");
        $gotoCookie = $request->cookies->get("site_login_goto");
        $url = $goto?$goto:($gotoCookie?$gotoCookie:"/");
        $data['mobile'] = $mobile;
        $data['goto'] = $url;
        $data['route'] = $route;
        $data['sidebar'] = $sidebar;
        $data['kw'] = $kw;
        return $this->render('@WebsiteBundle/index/header.html.twig', $data);
    }

    
    public function pageAction($id, PageService $pageService){
        $page = $pageService->getById($id);
        $data = [];
        $data["page"] = $page;
        return $this->render('@WebsiteBundle/index/page.html.twig', $data);
    }

    
    public function footerAction(PageService $pageService){
        $data = [];
        $data['copyright'] = $pageService->getOption("app.copyright");
        $data['beian'] = $pageService->getOption("app.beian.number");
        return $this->render('@WebsiteBundle/index/footer.html.twig', $data);
    }
}
